package com.wuba.rxjavademo.lyz.day0429;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by liyaxing on 2016/4/29.
 */
public class Demo01 {

    public static void main(String[] ss){

        Demo01 demo = new Demo01() ;
        Observable<Integer> observable = demo.createObservable() ;
        observable.subscribe(new Subscriber<Integer>() {
            @Override
            public void onCompleted() {
                print("===onCompleted===");
            }

            @Override
            public void onError(Throwable e) {
                print("===onError=== e :" + e);
            }

            @Override
            public void onNext(Integer integer) {
                print("===onNext=== value :" + integer);
            }
        }) ;
    }

    private Observable<Integer> createObservable(){
        return Observable.create(subscriber -> {
            for (int i = 0; i < 10; i++) {
                if(i == 4){
//                    subscriber.onError(new Throwable("value == 4"));
                    subscriber.onCompleted();
                }
                subscriber.onNext(i);
            }
        }) ;
    }




    private static void print(Object obj){
        System.out.println(obj) ;
    }
}
